#!/bin/bash


function deploy-k8s(){

    echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` "部署镜像:"

    echo "curl -X POST -o  curl-result.txt -s -w  %{http_code} --max-time 6 --retry 3 ${env_mpApiUrl}/ops/k8s/deployment/deploy-with-log/${buildLogId}?admin-token=${adminToken}"
    code=$(curl -X POST -o  curl-result.txt -s -w  %{http_code} --max-time 6 --retry 3 ${env_mpApiUrl}/ops/k8s/deployment/deploy-with-log/${buildLogId}?admin-token=${adminToken})

    if [ "$code" == "000" ]; then
        echo "网络错误，请重试！"
        return 3;
    elif [ "$code" == "200" ]  &&  grep -q '"code":0' curl-result.txt  ; then
        echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` '部署镜像成功!'
        show-file  curl-result.txt 50
    else
        echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` "部署镜像失败!!! 返回: $code"
        show-file  curl-result.txt 100
        return 1
    fi

}


function update-nginx-conf(){
    if [ "$language" != "vue" ]; then
      return;
    fi

    echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` "设置 nginx configmap(如果是第一次部署会重启nginx):"

    echo "curl -X POST -o   curl-result.txt -s -w  %{http_code} --max-time 6 --retry 3 ${env_mpApiUrl}/ops/k8s/configmap/set-vue/${buildLogId}?admin-token=${adminToken}"
    code=$(curl -X POST -o  curl-result.txt -s -w  %{http_code} --max-time 6 --retry 3 ${env_mpApiUrl}/ops/k8s/configmap/set-vue/${buildLogId}?admin-token=${adminToken})

    if [ "$code" == "000" ]; then
        echo "网络错误，请重试！"
        return 3;
    elif [ "$code" == "200" ] &&  grep -q '"code":0' curl-result.txt ; then
        echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` '已设置 nginx configmap!'
        show-file  curl-result.txt 50
    else
        echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` "设置 nginx configmap 出错!!! 返回: $code"
        show-file  curl-result.txt 100
        return 1
    fi
}


function restart-nginx(){
    echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` "重启 k8s nginx, namespace: mp"


    echo "curl -X POST -o  curl-result.txt -H"Content-type:application/json" -s -w  %{http_code} --max-time 30 --retry 3 ${env_mpApiUrl}/ops/k8s/deployment/restart-nginx?admin-token=${adminToken}  -d '{"targetEnv":"${targetEnv}","namespace":"${clusterCode}"}'"
    code=$(curl -X POST -o  curl-result.txt -H"Content-type:application/json" -s -w  %{http_code} --max-time 30 --retry 3 ${env_mpApiUrl}/ops/k8s/deployment/restart-nginx?admin-token=${adminToken}  -d '{"targetEnv":"${targetEnv}","namespace":"${clusterCode}"}')


    if [ "$code" == "000" ]; then
        echo "网络错误，请重试！"
        return 3;
    elif [ "$code" == "200" ] &&  grep -q '"code":0' curl-result.txt ; then
        echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` '重启 nginx 成功! '
        show-file  curl-result.txt 50
    else
        echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` "重启 nginx 失败!!! 返回: $code"
        show-file  curl-result.txt 100
        return 1
    fi

}



function check-k8s-status(){

    echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` "检查K8s服务状态:"
    echo "curl  -o  curl-result.txt  -s -w %{http_code} --max-time 6 --retry 3 ${env_mpApiUrl}/ops/k8s/deployment/status/${buildLogId}?admin-token=${adminToken}"

    for i in {1..100} ; do
        code=$(curl  -o  curl-result.txt  -s -w %{http_code} --max-time 6 --retry 3 ${env_mpApiUrl}/ops/k8s/deployment/status/${buildLogId}?admin-token=${adminToken} )


        if [ "$code" == "200" ] &&  grep -q '"code":0' curl-result.txt ; then
            echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` "启动服务成功!"
            show-file  curl-result.txt 50 ; echo
            break;
        else
            if [ $i -ge 50 ]; then
                echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` "启动服务失败!!! 返回: $code"
                show-file  curl-result.txt 100 ; echo

                return 1
            fi
            sleep 5
        fi
    done

}


function peek-from-docker(){
  echo "准备 docker-dir/docker_tar_dir 工作目录"
  ${sudo} chmod a+rw ./ -R
  rm -rf docker-dir &&  mkdir -p docker-dir/docker_tar_dir

  cd docker-dir && echo "文件夹切换到:`pwd`"

    echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` "打包镜像:"

    echo "${sudo} docker save -o docker.tar ${imageTagPrefix}$serviceName:$dockerImageVersion"
    ${sudo} docker save -o docker.tar ${imageTagPrefix}$serviceName:$dockerImageVersion

    if [ $? -ne 0 ]; then
        echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` '打包镜像错误!!!'
        return 1
    else
        echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` '打包镜像成功!'
    fi

  cd -
}

function unzip-docker-tar() {
  cd docker-dir && echo "文件夹切换到:`pwd`"

    echo "解压缩镜像"
    echo "tar xf  docker.tar  --warning=no-timestamp  &&  rm -rf docker.tar"
    tar xf  docker.tar  --warning=no-timestamp  &&  rm -rf docker.tar

    echo "根据 manifest.json 解压缩各镜像层"
    echo "cat manifest.json | curl -X POST -o  ops-tar-docker-layers.sh -s -w  %{http_code} --max-time 6 --retry 3 -H"Content-type:text/plain" --data-binary @- ${env_mpApiUrl}/ops/cmd/tar-docker-layers?admin-token=${adminToken}&target=docker_tar_dir"
    code=$(cat manifest.json | curl -X POST -o  ops-tar-docker-layers.sh -s -w  %{http_code} --max-time 6 --retry 3 -H"Content-type:text/plain" --data-binary @- ${env_mpApiUrl}/ops/cmd/tar-docker-layers?admin-token=${adminToken}&target=docker_tar_dir )


    if [ "$code" == "000" ]; then
        echo "网络错误，请重试！"
        return 3;
    elif [ "$code" != "200" ]; then
        echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` "tar-docker-layers接口错误!!! 返回: $code"
        return 1
    fi

    echo "bash ops-tar-docker-layers.sh"
    bash ops-tar-docker-layers.sh && echo "解压缩Docker完成" || ( echo "解压缩Docker失败!" ; return 1)


    echo "把镜像中的 app 文件，移动到 ../docker-app"
    mv  docker_tar_dir/app ../docker-app
  cd -

}

